(function(){var TopicApp = angular.module('TopicApp', []);

TopicApp.controller('TopicController', ['$scope','$http','$sce','$interval','$rootScope',function($scope,$http,$sce,$interval,$rootScope) {
	
	var currentTopic = (location.search.split('='))[1];
	$rootScope.isNewMessages = true;
	$rootScope.TopicTenMessagesArray;
	$rootScope.OnlyOnce=true;
	$rootScope.showtopicupdatebutton = false;
	
	$('#topicName').html(currentTopic);

	
		$.post("GetTopicTenLastMessages",
				{Topic : currentTopic  }
				).then(function(response) {
					$rootScope.TopicTenMessagesArray = JSON.parse(response);
					$rootScope.test = $interval($scope.checkForUpdate, 200,[1]);
					$rootScope.isAllMessages=true;
				},
				
				    function(returnedData){
				         console.log(returnedData);
				});
	
		$scope.GoToHomePage = function() {
			window.location = '\main.html';
		}
   

   $scope.checkForUpdate = function(){
//  	 if($rootScope.isNewMessages){
	   if($rootScope.OnlyOnce){
		   $interval.cancel($rootScope.test);
		   $rootScope.OnlyOnce = false;
	   }
	      $.post("GetTopicTenLastMessages",
				  {Topic : currentTopic }
				).then(function(response) {
					var countToTen = 0;
					var MessagesArray = JSON.parse(response);
					for(message in MessagesArray){
						for(currentmessage in $rootScope.TopicTenMessagesArray){
							if($rootScope.TopicTenMessagesArray[currentmessage].MessageId == MessagesArray[message].MessageId){
								$rootScope.isNewMessages = false;
							}
						}
						if($rootScope.isNewMessages){
							break;
						}else{
							$rootScope.isNewMessages = true;
							countToTen++;
							
						}
					}
				      if((countToTen < MessagesArray.length)){
				    	  $rootScope.showtopicupdatebutton = true;
				      }
	            },
					
					    function(returnedData){
					         console.log(returnedData);
					});

	      $interval($scope.checkForUpdate, 20000);
  	 
  	 }
   
   
   $scope.update = function(){
	      $.post("GetTopicTenLastMessages",
				  { Topic : currentTopic }
				).then(function(response) {
					
					var updateMessagesArray = JSON.parse(response);
					$rootScope.TopicTenMessagesArray = updateMessagesArray;
	            },
					
					    function(returnedData){
					         console.log(returnedData);
					});
   	}
   
   //this function is responiable for retuning how much time have pased from
   //the publish of the message.
   $scope.getTime = function(string){
 	  var d = new Date(string);
 	  var now = new Date();
 	  var returntext;
 	  var milicesoundsdiff = (now.getTime() - d.getTime());
 	  if((milicesoundsdiff / 1000)< 60){
 		  returntext = (Math.round(milicesoundsdiff / 1000)).toString() +' seconds ago';
 		  return $sce.trustAsHtml(returntext);
 	  }else if((milicesoundsdiff / 60000) < 60){
 		  returntext = (Math.round(milicesoundsdiff / 60000)).toString() +' minutes ago'; 
 		  return $sce.trustAsHtml(returntext);
 	  }else if((milicesoundsdiff / 3600000) < 24){
			  returntext = (Math.round(milicesoundsdiff / 3600000)).toString() +' hours ago'; 
			  return $sce.trustAsHtml(returntext);
 	  }else{
 		  returntext = d.getDate().toString() + '/' +d.getMonth().toString() + '/' + d.getFullYear() + ' at' +d.getHours() + ':' +d.getMinutes();
 		  return $sce.trustAsHtml(returntext);
 	  }
 	  
 	  return d;
   }
   
 //this function is to turn the returned messages into
   //a html.
   $scope.setcheck = function(text){
 	  
 	  return $sce.trustAsHtml(text);
   }
}]);

TopicApp.filter('MessageDate',function(){
	return function(string){
		var d = new Date(string);
	 	  var now = new Date();
	 	  var returntext;
	 	  var milicesoundsdiff = (now.getTime() - d.getTime());
	 	  if((milicesoundsdiff / 1000)< 60){
	 		  returntext = (Math.round(milicesoundsdiff / 1000)).toString() +' seconds ago';
	 		  return returntext;
	 	  }else if((milicesoundsdiff / 60000) < 60){
	 		  returntext = (Math.round(milicesoundsdiff / 60000)).toString() +' minutes ago'; 
	 		  return returntext;
	 	  }else if((milicesoundsdiff / 3600000) < 24){
				  returntext = (Math.round(milicesoundsdiff / 3600000)).toString() +' hours ago'; 
				  return returntext;
	 	  }else{
	 		  returntext = d.getDate().toString() + '/' +d.getMonth().toString() + '/' + d.getFullYear() + ' at' +d.getHours() + ':' +d.getMinutes();
	 		  return returntext;
	 	  }
	 	  
	 	  return d;
	   }
	
})

})();